<?php
class HomeView
{
    public function show()
    {
        echo "Displaying Home Page\r\n";
    }
}
class StudentView
{
    public function show()
    {
        echo "Displaying Student Page\r\n";
    }
}
class Dispatcher
{
    protected $student=null;
    protected $home=null;
    public function __construct()
    {
        $this->student = new StudentView;
        $this->home = new HomeView;
    }
    public function dispatch($request)
    {
        if(strtolower($request) === "student"){
            $this->student->show();
        }
        else{
            $this->home->show();
        }
    }
}

class FrontController
{
    protected $dispatcher;
    public function __construct()
    {
        $this->dispatcher = new Dispatcher;
    }
    protected function isAuthenticUser()
    {
        echo "User is authenticated successfully.\r\n";
        return true;
    }
    /** 记录请求 */
    protected function trackRequest($request)
    {
        echo "Page requested: $request\r\n";
    }
  
    public function dispatchRequest($request)
    {
       //记录每一个请求
        $this->trackRequest($request);
        //对用户进行身份验证
        if($this->isAuthenticUser()){
            $this->dispatcher->dispatch($request);
        }
    }
}

header('Content-Type: text/plain');

$fc = new FrontController;
$fc->dispatchRequest("home");
$fc->dispatchRequest("student");
